package com.servlet.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import DB.DBManager;

import com.servlet.vo.bean.TradeLog;

public class TradeLogDao {

	private Connection con;
	private Statement statement;
	private ResultSet resultSet;
	private boolean flag = false;
	private StringBuffer sql = new StringBuffer();
	private final String TABLE = "tradelog";
	
	public TradeLogDao() throws SQLException{
		con = DBManager.getConn();
		statement = con.createStatement();
	}
	public void close(){
		
		if(null != resultSet){
			try {
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(null != statement){
			try {
				statement.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(null != con){
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public boolean save(TradeLog obj) {
		sql.append("INSERT INTO " + TABLE + "(ADDTIME,deleteStatus,trade_desp,trade_type,");
		if(obj.getCorrect_trade() != null){
			sql.append("correct_trade_id,");// 冲正 的记录id
		}
		sql.append("amount,carrier_id,acc_id,carrierAvailableBalance,accountAvailableBalance) VALUES( NOW(),FALSE,'");
		sql.append(obj.getTrade_desp() + "'," + obj.getTrade_type() + ",");
		if(obj.getCorrect_trade() != null){
			sql.append(obj.getCorrect_trade() + ",");
		}
		sql.append(obj.getAmount() + "," + obj.getCarrier() + "," + obj.getAcc() + ",");
		sql.append(obj.getCarrierAvailableBalance() + "," + obj.getAccountAvailableBalance() + ")");
		
		try {
			flag = statement.executeUpdate(sql.toString()) == 1;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			flag = true;
		}finally{
			close();
		}
		return flag;
	}
	
	public boolean delete(Long id){
		
		sql.append("UPDATE " + TABLE + " SET deleteStatus = TRUE WHERE id = '" + id + "'");
		try {
			flag = statement.execute(sql.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return flag;
	}
	
	public TradeLog getObj(long id){
		TradeLog obj = null;
		sql.append("SELECT id,addTime,acc_id,carrier_id,trade_desp,trade_type,correct_trade_id,amount FROM " + TABLE );
		sql.append(" WHERE deleteStatus = FALSE and id = " + id);
		try {
			resultSet = statement.executeQuery(sql.toString());
			if(resultSet.next()){
				obj = new TradeLog();
				obj.setId(resultSet.getLong("id"));
				obj.setAddTime(resultSet.getTimestamp("addTime"));
				obj.setAcc(resultSet.getLong("acc_id"));
				obj.setCarrier(resultSet.getLong("carrier_id"));
				obj.setTrade_desp(resultSet.getString("trade_desp"));
				obj.setTrade_type(resultSet.getInt("trade_type"));
				obj.setCorrect_trade(resultSet.getLong("correct_trade_id"));
				obj.setAmount(resultSet.getBigDecimal("amount"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return obj;
	}
	
	public List<TradeLog> getList(){
		
		sql.append("SELECT id,addTime,acc_id,carrier_id,trade_desp,trade_type,correct_trade_id,amount FROM " + TABLE + " WHERE deleteStatus = FALSE ");
		sql.append(" order by addTime desc ");
		List<TradeLog> objs = new ArrayList<TradeLog>();
		try {
			resultSet = statement.executeQuery(sql.toString());
			TradeLog obj = null;
			while(resultSet.next()){
			
				obj = new TradeLog();
				obj.setId(resultSet.getLong("id"));
				obj.setAddTime(resultSet.getTimestamp("addTime"));
				obj.setAcc(resultSet.getLong("acc_id"));
				obj.setCarrier(resultSet.getLong("carrier_id"));
				obj.setTrade_desp(resultSet.getString("trade_desp"));
				obj.setTrade_type(resultSet.getInt("trade_type"));
				obj.setCorrect_trade(resultSet.getLong("correct_trade_id"));
				obj.setAmount(resultSet.getBigDecimal("amount"));
				objs.add(obj);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return objs;
	}
	
	/**
	 * 通过 属性值 获取 账单集合
	 * @param params
	 * @return
	 */
	public List<TradeLog> getListByProperties(Map<String, String> params){
		
		sql.append("SELECT a.id,a.addTime,a.deleteStatus,a.acc_id,a.carrier_id,a.trade_desp,a.trade_type,");
		sql.append("a.correct_trade_id,a.amount,a.carrierAvailableBalance,a.accountAvailableBalance,");
		sql.append("ac.ac_name,c.ca_name ");
		sql.append(" FROM " + TABLE + " a");
		sql.append(" LEFT JOIN account ac ON a.acc_id = ac.id ");
		sql.append(" LEFT JOIN carrier c  ON a.carrier_id = c.id ");
		sql.append("WHERE a.deleteStatus = FALSE ");
		Iterator<String> iterator = params.keySet().iterator();
		while(iterator.hasNext()){
			String key = iterator.next();
			String value = params.get(key);
			sql.append(" and "+key+"="+value);
		}
		sql.append(" order by addTime desc");
		List<TradeLog> objs = new ArrayList<TradeLog>();
		try {
			resultSet = statement.executeQuery(sql.toString());
			TradeLog obj = null;
			while(resultSet.next()){
				obj = new TradeLog();
				obj.setId(resultSet.getLong("id"));
				obj.setAddTime(resultSet.getTimestamp("addTime"));
				obj.setDeleteStatus(resultSet.getBoolean("deleteStatus"));
				obj.setAcc(resultSet.getLong("acc_id"));
				obj.setCarrier(resultSet.getLong("carrier_id"));
				obj.setTrade_desp(resultSet.getString("trade_desp"));
				obj.setTrade_type(resultSet.getInt("trade_type"));
				obj.setCorrect_trade(resultSet.getLong("correct_trade_id"));
				obj.setAmount(resultSet.getBigDecimal("amount"));
				obj.setCarrierAvailableBalance(resultSet.getBigDecimal("carrierAvailableBalance"));
				obj.setAccountAvailableBalance(resultSet.getBigDecimal("accountAvailableBalance"));
				obj.setAccName(resultSet.getString("ac_name"));
				obj.setCarrierName(resultSet.getString("ca_name"));
				objs.add(obj);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return objs;
	}
}